package com.kaluli.project.controller;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.phprpc.util.AssocArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kaluli.project.domain.entity.KllProduct;
import com.kaluli.project.domain.entity.KllProductSku;
import com.kaluli.project.service.KllMainOrderService;
import com.kaluli.project.service.KllProductService;
import com.kaluli.project.utils.SerializeUtil;

/**
 * 
 * 商品信息
 * 
 * @author niwei
 * 
 */
@Controller
public class ItemController {

	private static Logger log = Logger.getLogger(ItemController.class);

	@Autowired
	private Environment env;

	@Autowired
	private KllProductService kllProductService;
	
	@Autowired
	private KllMainOrderService kllMainOrderService;

	@RequestMapping(value = "/findSkuByNumber")
	@ResponseBody
	public ModelAndView findSkuByNumber(@RequestParam(value = "productId", required = true) Long productId) throws NoSuchAlgorithmException {
		Map<String, Object> map = new HashMap<>();
		String result = "";

		if (productId == null) {
//			result = "{'code':'error','message':'商品编号为空!'}";
			map.put("code", "error");
			map.put("message", "商品编号为空!");
			map.put("item", null);
			return new ModelAndView(new MappingJacksonJsonView(),map);
		}
		KllProduct product = kllProductService.findItems(productId);
		if (product != null) {
			//遍历product 反序列化相关信息
			for(KllProductSku s:product.getItemSkus()){
				s.setKllProduct(null);
				String attr = s.getAttr();
				if(StringUtils.isNotBlank(attr)){
					s.setAttr(JSON.toJSONString(SerializeUtil.serializeAttr(attr)));
				}
			}
			String picDetail = product.getItemAttr().getPicDetail();
			String attrs = product.getItemAttr().getAttrs();
			if(StringUtils.isNotBlank(picDetail)){
				Map<Integer, String> mapPic = SerializeUtil.serializeInt(picDetail);
				product.getItemAttr().setPicDetail(JSON.toJSONString(mapPic));
			}
			if(StringUtils.isNotBlank(attrs)){
				Map<String, String> mapAttrs = SerializeUtil.serialize(attrs);
				product.getItemAttr().setAttrs(JSON.toJSONString(mapAttrs));
			}
			result=JSON.toJSONString(product);
			JSONObject js = JSON.parseObject(result);
			
		} else {
//			result = "{'code':'error','message':'获取商品信息失败!'}";
			map.put("code", "error");
			map.put("message", "获取商品信息失败!");
			map.put("item", null);
			return new ModelAndView(new MappingJacksonJsonView(),map);
		}

//		log.info(result);
		JSONObject js = JSON.parseObject(result);
		map.put("item", js);
		map.put("code", "success");
		map.put("message", "获取数据成功!");

		return new ModelAndView(new MappingJacksonJsonView(),map);

	}


	public static void main(String[] args) {
		String attr= "a:3:{i:0;s:30:\"京东同步物流单号操作\";i:1;N;i:2;N;}";
		System.out.println(SerializeUtil.serializeAttr(attr));
	}

}
